<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
include_once "../resources/org/code/vcode.class.php";

class LoginController extends Controller{
    public function login(){
        return view('admin/login/login');
    }
    public function code(){
        $vcode = new \Vcode(100,50,4);
        Session::put("code",$vcode->getcode());
        $vcode->outimg();
        dump($vcode);
    }
    public function check(Request $request){
        $code = $request->code;
        $sessioncode = Session::get("code");
        if(strtoupper($code)==strtoupper(($sessioncode))){
            $admin_name = $request->username;
            $admin_pwd = md5($request->password);
            $admin = DB::table("admin")->whereRaw("admin_name=? AND admin_pwd=?",["$admin_name","$admin_pwd"])->first();
            if($admin){
                Session::put("isadmin","1");
                Cookie::queue("name",$admin_name,60);//60分钟
                return 1;die;
            }else{
                return 2;die;
            }
        }else{
            return 3;die;
        }
        
    }
    public function logout(){
        Cookie::queue(Cookie::forget("name"));
        Session::forget("isadmin");
        return redirect('/admin/login');
    }
}